package first_practice;

import java.util.HashMap;
import java.util.Map;

public class Solution1 {
    public int[] twoSum(int[] nums, int target) {
        int[] ans = {-1, -1};
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int diff = target - nums[i];
            if (map.containsKey(diff)) {
                // 如果diff在map中，则当前元素和diff的value值即为ans
                ans[0] = i;
                ans[1] = map.get(diff);
                return ans;
            } else map.put(nums[i], i); // map中保存nums的元素及其下标
        }
        return ans;
    }
}